package ${package.Controller};

import com.quyeying.utils.result.R;
import com.quyeying.utils.result.ResultEnum;
import org.springframework.web.bind.annotation.*;
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

/**
* <p>
* ${table.name} 前端控制器
* </p>
* @author ${author}
* @since ${date}
*/
@Slf4j
<#if restControllerStyle>@RestController
<#else>@Controller
</#if>
@RequestMapping("${table.entityPath?substring(3)?uncap_first}")
<#if kotlin>class ${table.controllerName}<#if superControllerClass??>:${superControllerClass}()</#if><#else><#if superControllerClass??>public class ${table.controllerName} extends ${superControllerClass}{<#else>public class ${table.controllerName} {</#if>

    @Resource
    private ${table.serviceName} ${(table.serviceName)?uncap_first};

    /**
    * 新增
    **/
    @PostMapping("/add")
    public R add(@RequestBody ${entity} ${entity?uncap_first}){
        try {
            return ${(table.serviceName)?uncap_first}.add(${entity?uncap_first});
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 删除
    **/
    @DeleteMapping("/del")
    public R delete(Long id){
        try {
            return ${(table.serviceName)?uncap_first}.delete(id);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 更新
    **/
    @PostMapping("/update")
    public R update(@RequestBody ${entity} ${entity?uncap_first}){
        try {
            return ${(table.serviceName)?uncap_first}.updateData(${entity?uncap_first});
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 分页查询
    **/
    @GetMapping("/findListByPage")
    public R findListByPage(@RequestParam Integer current,
    @RequestParam Integer size){
        try {
            return ${(table.serviceName)?uncap_first}.findListByPage(current, size);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * id查询
    **/
    @GetMapping("/findById")
    public R findById(Long id){
        try {
            return ${(table.serviceName)?uncap_first}.findById(id);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }
}
</#if>